/* Examples/cauchy_01.cpp
 * James S. Plank

Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
Copright (C) 2007 James S. Plank

James S. Plank
Department of Electrical Engineering and Computer Science
University of Tennessee
Knoxville, TN 37996
plank@cs.utk.edu
*/

/*
 * $Revision: 1.2 $
 * $Date: 2008/08/19 17:41:40 $
 */

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jerasure-2.h"
#include "cauchy-2.h"
#include "reed_sol-2.h"

#define talloc(type, num) (type *) malloc(sizeof(type)*(num))

void usage(char *s)
{
  fprintf(stderr, "usage: cauchy_01 n w - Prints the number of ones in the bitmatrix representation of n in GF(2^w).\n");
  fprintf(stderr, "       \n");
  fprintf(stderr, "       It also prints out the bit-matrix and confirms that the number of ones is correct.\n");
  fprintf(stderr, "       \n");
  fprintf(stderr, "This demonstrates: cauchy_n_ones()\n");
  fprintf(stderr, "                   JER_Bitmatrix::JER_Bitmatrix( &JER_Matrix )\n");
  fprintf(stderr, "                   JER_Bitmatrix::Print()\n");
  if (s != NULL) fprintf(stderr, "%s\n", s);
  exit(1);
}

int main(int argc, char **argv)
{
  int n, i, no, w, j;
	JER_Bitmatrix *jbm;
	JER_Matrix *jm;
  
  if (argc != 3) usage(NULL);
  if (sscanf(argv[1], "%d", &n) == 0 || n <= 0) usage((char *)"Bad n");
  if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 32) usage((char *)"Bad w");
  if (w < 30 && n >= (1 << w)) usage((char *)"n is too big");

	jm = new JER_Matrix(1, 1, w);
	jm->Elts[0][0] = n;
	jbm = new JER_Bitmatrix(*jm);

  no = 0;
	for (i = 0; i < w; i++) 
		for (j = 0; j < w; j++) {
		  if (jbm->Isset(i, j)) no++;
		}

  printf("# Ones: %d\n", cauchy_n_ones(n, w));
  printf("\n");
  printf("Bitmatrix has %d ones\n", no);
  printf("\n");
	jbm->Print();

  return 0;
}
